"""
@Author  : 康帅
@Time    : 2021/5/8 11:14
@Function: 装饰器神器decorator
"""
import logging
from decorator import decorator
import time


def deco(func):
    """ 1.常规装饰器(不带参数) """

    def wrapper(*args, **kw):
        print("Ready to run task")
        func(*args, **kw)
        print("Successful to run task")

    return wrapper


@decorator
def deco(func, *args, **kwargs):
    """ 2.第三方装饰器(不带参数) """
    print("Ready to run task")
    func(*args, **kwargs)
    print("Successful to run task")


@decorator
def warn_slow(func, timelimit=60, *args, **kw):
    """ 3.带参数的装饰器 """
    t0 = time.time()
    result = func(*args, **kw)
    dt = time.time() - t0
    if dt > timelimit:
        logging.warn('%s took %d seconds', func.__name__, dt)
    else:
        logging.info('%s took %d seconds', func.__name__, dt)
    return result


@deco
def my_func():
    print("Running the task")


@warn_slow(timelimit=600)  # warn if it takes more than 10 minutes
def run_calculation(tempdir, outdir):
    pass


my_func()
run_calculation()
